package choctaw;
import java.awt.Color;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JButton;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import javax.swing.JDialog;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class Choctaw extends JFrame {
	private JPanel mainPanel;
	private JDialog initialDialog;
	private JPanel initialDialogPanel;
	private JButton initialDialogExitBtn;
	private JButton initialDialogOk;
	private JTextField userNameTxtFld;
	private JLabel nameLabel;
	public String userName;
	
	public String multicast_group;// = "224.1.1.94";
	public String multicast_port;// = "8633";
	public String file_separator = System.getProperty("file.separator");
	public String choctaw_path = System.getProperty("java.class.path") + file_separator + ".." + file_separator;
	public static void main(String[] args) {
		Choctaw c = new Choctaw();
		c.getProperties();
		c.initGUI();
		
	}
	
	private void getProperties() {
		Properties p = new Properties();
		try { 
			p.load(new FileInputStream(choctaw_path + "choctaw.properties"));
			multicast_group = p.getProperty("multicast_group");
			System.out.println(multicast_group);
			multicast_port = p.getProperty("multicast_port");
			System.out.println(multicast_port);
		} catch (IOException e) 
		{ 
			e.printStackTrace();
			multicast_group = "224.1.1.94";
			multicast_port = "8633";
		} 
		
		
	}

	private void initGUI()
	{
		AnchorLayout thisLayout = new AnchorLayout();
		getContentPane().setLayout(thisLayout);
		this.setTitle("Choctaw");
		mainPanel = new JPanel();
		getContentPane().add(mainPanel, new AnchorConstraint(1, 1002, 1001, 2, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
		mainPanel.setPreferredSize(new java.awt.Dimension(183, 277));

		this.setSize(243, 470);
		// Get the size of the screen 
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		// Determine the new location of the window 
		int w = this.getSize().width; 
		int h = this.getSize().height; 
		int x = (dim.width-w)/2; 
		int y = (dim.height-h)/2; // Move the window 
		this.setLocation(x, y); 
		this.setVisible(true);
		this.getInitialDialog();
		

	}
	
	private JDialog getInitialDialog() {
		if(initialDialog == null) {
			initialDialog = new JDialog(this,ModalityType.APPLICATION_MODAL);
			AnchorLayout initialDialogLayout = new AnchorLayout();
			initialDialog.setLayout(initialDialogLayout);
			
			initialDialog.setTitle("User Name");
			initialDialog.getContentPane().setBackground(new java.awt.Color(230,230,230));
			initialDialog.getContentPane().add(getJPanel1(), new AnchorConstraint(4, 1003, 1004, 3, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			initialDialog.setSize(498, 344);
		}
		// Get the size of the screen 
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		// Determine the new location of the window 
		int w = initialDialog.getSize().width; 
		int h = initialDialog.getSize().height; 
		int x = (dim.width-w)/2; 
		int y = (dim.height-h)/2; // Move the window 
		initialDialog.setLocation(x, y); 
		
		initialDialog.setUndecorated(true);
	//	initialDialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 
		initialDialog.setVisible(true);
		initialDialog.setResizable(true);
		initialDialog.setPreferredSize(new java.awt.Dimension(498, 344));
		return initialDialog;
	}
	
	private JLabel getNameLabel() {
		if(nameLabel == null) {
			nameLabel = new JLabel();
			AnchorLayout initialDialogOkLayout = new AnchorLayout();
			nameLabel.setText("Your User Name (visible to others)");
			nameLabel.setLayout(null);
			nameLabel.setFont(new java.awt.Font("Verdana",1,14));
			nameLabel.setPreferredSize(new java.awt.Dimension(408, 22));
		}
		return nameLabel;
	}
	
	private JTextField getUserName() {
		if(userNameTxtFld == null) {
			userNameTxtFld = new JTextField();
			userNameTxtFld.setPreferredSize(new java.awt.Dimension(408, 70));
			userNameTxtFld.setFont(new java.awt.Font("Verdana",3,14));
			userNameTxtFld.setMargin(new java.awt.Insets(1, 10, 2, 1));
			userNameTxtFld.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent evt) {
					userNameTxtFldKeyReleased(evt);
				}
			});
		}
		return userNameTxtFld;
	}
	
	private JButton getInitialDialogOk() {
		if(initialDialogOk == null) {
			initialDialogOk = new JButton();
			initialDialogOk.setLayout(null);
			initialDialogOk.setText("OK");
			initialDialogOk.setPreferredSize(new java.awt.Dimension(408, 70));
			initialDialogOk.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					initialDialogOkActionPerformed(evt);
				}
			});
		}
		return initialDialogOk;
	}
	
	private void initialDialogOkActionPerformed(ActionEvent evt) {
		userName = userNameTxtFld.getText();
		if(userName.equals(""))
		{
			nameLabel.setText("Error: User Name Cannot Be Blank!");
			nameLabel.setForeground(new Color(255,0,0));
			return;
		}
		userName += "_" + System.currentTimeMillis();
		System.out.println(userName);
		initialDialog.dispose();
	}
	
	private void userNameTxtFldKeyReleased(KeyEvent evt) {
		if(evt.getKeyCode() == KeyEvent.VK_ENTER)
		{
			this.initialDialogOkActionPerformed(null);
		}
	}
	
	private JButton getInitialDialogExitBtn() {
		if(initialDialogExitBtn == null) {
			initialDialogExitBtn = new JButton();
			AnchorLayout initialDialogExitBtnLayout = new AnchorLayout();
			initialDialogExitBtn.setLayout(null);
			initialDialogExitBtn.setText("Exit");
			initialDialogExitBtn.setPreferredSize(new java.awt.Dimension(408, 69));
			initialDialogExitBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					initialDialogExitBtnActionPerformed(evt);
				}
			});
		}
		return initialDialogExitBtn;
	}
	
	private void initialDialogExitBtnActionPerformed(ActionEvent evt) {
		this.dispose();
	}
	
	private JPanel getJPanel1() {
		if(initialDialogPanel == null) {
			initialDialogPanel = new JPanel();
			AnchorLayout initialDialogPanelLayout = new AnchorLayout();
			initialDialogPanel.setLayout(initialDialogPanelLayout);
			initialDialogPanel.setPreferredSize(new java.awt.Dimension(498, 344));
			initialDialogPanel.setBorder(new LineBorder(new java.awt.Color(0,0,0), 10, false));
			initialDialogPanel.add(getNameLabel(), new AnchorConstraint(79, 914, 143, 95, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			initialDialogPanel.add(getUserName(), new AnchorConstraint(178, 914, 382, 95, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			initialDialogPanel.add(getInitialDialogOk(), new AnchorConstraint(452, 914, 655, 95, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			initialDialogPanel.add(getInitialDialogExitBtn(), new AnchorConstraint(716, 914, 917, 95, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
		}
		return initialDialogPanel;
	}

}
